package Domain;

import org.apache.log4j.Logger;

import java.io.Serializable;
import java.util.LinkedList;

import Domain.DataStructures.IUserData;

/**
 * This class contains all the information about a user in the system
 */
public class User implements IUserData,Serializable {

    static Logger logger            = Logger.getLogger("MovieSystem.Domain.User");

    public static int OFFLINE = 0;
    public static int ONLINE = 1;
    public static int SESSION_EXPIRED = 2;
    public static int SESSION_DROPPED = 3;

    private String m_username; //unique id
    private String m_firstname;
    private String m_lastname;
    private String m_encryptedPassword;
    private boolean m_VIP;
    private String m_email;
    private String m_acl;
    private int m_state;
    private Object m_terminal;
    private LinkedList<Genre> m_favGenres;
    private LinkedList<Actor> m_favActors;
    private LinkedList<String> m_favDirectors;

    /* constructor */
    public User(String m_username, String m_firstname, String m_lastname, String m_password, boolean m_VIP, String m_email, String m_acl) {
        this.m_username = m_username.trim();
        this.m_firstname = m_firstname.trim();
        this.m_lastname = m_lastname.trim();
        this.m_encryptedPassword = m_password;
        this.m_VIP = m_VIP;
        this.m_email = m_email.trim();
        this.m_acl = m_acl;

        this.m_favGenres = new LinkedList<Genre>();
        this.m_favActors = new LinkedList<Actor>();
        this.m_favDirectors = new LinkedList<String>();

        this.m_state=User.OFFLINE;
        this.m_terminal = null;
    }

    /* Getters & Setters */
    public String getUsername() {
        return m_username;
    }

    public void setUsername(String m_username) {
        this.m_username = m_username;
    }

    public String getFirstname() {
        return m_firstname;
    }

    public void setFirstname(String m_firstname) {
        this.m_firstname = m_firstname;
    }

    public String getLastname() {
        return m_lastname;
    }

    public void setLastname(String m_lastname) {
        this.m_lastname = m_lastname;
    }

    public String getEncryptedPassword() {
        return m_encryptedPassword;
    }

    public void setPassword(String m_encryptedPassword) {
        this.m_encryptedPassword = m_encryptedPassword;
    }

    public boolean isVIP() {
        return m_VIP;
    }

    public void setVIP(boolean m_VIP) {
        this.m_VIP = m_VIP;
    }

    public String getEmail() {
        return m_email;
    }

    public boolean isManager() {
        return m_acl.equals("Manager");
    }

    public boolean isAdministrator() {
        return m_acl.equals("Admin");
    }

    public boolean isConnected() {
        return (m_state == ONLINE);
    }

    public void setEmail(String m_email) {
        this.m_email = m_email;
    }

    public String getACL() {
        return m_acl;
    }

    public void setACL(String m_acl) {
        this.m_acl = m_acl;
    }

    public int getState() {
        return m_state;
    }

    public void setState(Object m_terminal,int m_state) {
        this.m_state = m_state;
        this.m_terminal = m_terminal;
    }

    public LinkedList<Genre> getFavGenres() {
        return m_favGenres;
    }

    public LinkedList<Actor> getFavActors() {
        return m_favActors;
    }

    public LinkedList<String> getFavDirectors() {
        return m_favDirectors;
    }

    public Object getTerminal() {
        return m_terminal;
    }

    /* Methods */
    public void addGenre(Genre genre){
        m_favGenres.add(genre);
    }

    public void addActor(Actor actor){
        m_favActors.add(actor);
    }

    public void addDirector(String director){
        m_favDirectors.add(director);
    }

    public boolean isMatch(String keyword, SearchUserMethods method) {
        boolean res;
        switch(method){
            case EXACT_NAME:
                res = this.m_username.equals(keyword);      break;
            case LOGGED:
                res = this.m_state == ONLINE;  break;
            case ALL:
                res = true; break;
            default:
                res=false;
                logger.warn("Checking isMatch("+keyword+","+method+") for an unsupported method!");
        }
        logger.debug("Checking isMatch("+keyword+","+method+") for user "+this.m_username+" is : "+res);
        return res;
    }
}
